<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bannerwarring extends Controller
{
	private $_template_f;
	private $_lang;
	private $_numItem = 20;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$this->_template_f = $this->config->item('template_f');
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$this->load->model('bannerwarring/bannerwarring_model');
		$this->load->language('common', $this->_lang);
		$this->load->language('bannerwarring/bannerwarring', $this->_lang);
	}
	
	function index()
	{
		$data = array();
		$data['items'] = $this->bannerwarring_model->getAll();
		$data['lang'] = $this->lang->line('bannerwarring');
		$data['arrLocation'] = $this->lang->line('common_lang');
		$data['arrLocation'] = $data['arrLocation']['arr_location'];
		
		//Nhom cac banner cung campaign
		$colspan = array();
		for($i = 0; $i < count($data['items']); $i++) $colspan[] = $data['items'][$i]->campaignid;
		$colspan = array_count_values($colspan);
		$data['colspan'] = $colspan;

		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'bannerwarring/index', $data);
		
		$this->_loadFooter();
	}
	
	function saveWarring()
	{
		$lang = $this->lang->line('bannerwarring');
		if(!isset($_POST['id']))
		{
			echo json_encode(array('status'=>0, 'mess'=>$lang['save_not_success']));
			return;
		}
		
		$id = (int)$this->input->post('id',true);
		$text = $this->mycommon->getSaveSqlStr($this->input->post('text',true));
		//$text = $this->input->post('text',true);
		
		if(trim(strip_tags($text)) == '')
		{
			echo json_encode(array('status'=>0, 'mess'=>$lang['enter_warring']));
			return;
		}
		
		$this->bannerwarring_model->saveWarring($id, $text);
		echo json_encode(array('status'=>1, 'mess'=>$lang['save_success']));
	}
		
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
}